package com.foreveross.chameleon.parser;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.foreveross.pojo.UserPrivilege;

public class UserPrivilegeParser {

	public UserPrivilege parser(String json) {

		UserPrivilege userPrivilege = new UserPrivilege();
		try {
			JSONObject jb = new JSONObject(json);
			String role=null;
			if (!jb.isNull("rolesTag")) {
				role = jb.getString("rolesTag");
			}
			JSONArray jay = null;
			if (!jb.isNull("priviliges")) {
				jay = jb.getJSONArray("priviliges");
			}
			ArrayList<String> getList = new ArrayList<String>();
			ArrayList<String> deleteList = new ArrayList<String>();
			ArrayList<String> privilegeList = new ArrayList<String>();
			if (jay != null) {
				int len = jay.length();
				for (int i = 0; i < len; i++) {
					JSONArray jay2 = jay.getJSONArray(i);
					if (!privilegeList.contains(jay2.getString(1))) {
						privilegeList.add(jay2.getString(1));
					}

					if (UserPrivilege.GET.equals(jay2.getString(0))) {
						getList.add(jay2.getString(1));
					}

					if (UserPrivilege.DELETE.equals(jay2.getString(0))) {
						deleteList.add(jay2.getString(1));
					}

				}
			}
			userPrivilege.setRole(role);
			userPrivilege.setDeleteList(deleteList);
			userPrivilege.setGetList(getList);
			userPrivilege.setPrivilegeList(privilegeList);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userPrivilege;
	}

}
